
#include "member_BListItem.h"
#include "member_BListWidget.h"

using namespace BWE;

member_BListItem::member_BListItem(BListItem* item)
{
	boss = 0;
	list = 0;
	ia_form = Form_None;
	ia_align = Align_LeftCenter;
	graph = Graph_None;
	visible = true;
	selected = false;
	dirty = true;
	index = -1;
}
member_BListItem::~member_BListItem()
{

}

void member_BListItem::dirtyCell()
{
	if (list)
	{
		dirty = true;
		list_member(list)->dirty = true;
		list->fresh();
	}
}
void member_BListItem::freshSize(const BFont* font)
{
	ia_imageSize = iconSize;
	if (font)
	{
		ia_textSize = font->textSize(text);
	}
	bool hasImage = icon || image || graph || (widget && widget->enabled());
	bool showImage = hasImage || (ia_form >= Form_IconOnly && ia_form <= Form_IconUnderText);
	if (ia_form == Form_IconAlone && !hasImage)
		showImage = false;
	if (ia_form == Form_TextAlone && text.empty() && hasImage)
		showImage = true;
	if (showImage)
	{
		if (font)
		{
			if (ia_imageSize.width() < 1)
				ia_imageSize.width() = font->size();
			if (ia_imageSize.height() < 1)
				ia_imageSize.height() = font->size();
		}
		if (widget && widget->visible())
		{
			BSize sizeHint = widget->sizeHint();
			if (ia_imageSize.width() < sizeHint.width())
				ia_imageSize.width() = sizeHint.width();
			if (ia_imageSize.height() < sizeHint.height())
				ia_imageSize.height() = sizeHint.height();
		}
	}
	else
	{
		ia_imageSize.reset();
	}
	if (list)
	{
		ia_spacing = list_member(list)->spacing;
	}
	adjust(BRect());
	hintSize = ia_hintSize;
}
void member_BListItem::adjustRect(int x, int y, int w, int h)
{
	rect.set(x, y, w, h);
	adjust(rect);
	if (widget)
	{
		//BRect abRect = rect + list->area().pos();
		//if (ia_form)
		//{
		//	abRect = ia_imageRect + list->area().pos();
		//}
		//widget->setVisible(abRect.valid());
		//widget->setRect(abRect);
		//if (ia_align && widget->size() != abRect.size())
		//{
		//	BRect realRect = abRect.align(ia_align, widget->size());
		//	widget->setRect(realRect);
		//}
		if (ia_form)
		{
			widget->setVisible(ia_imageRect.valid());
			widget->setRect(ia_imageRect);
			if (ia_align && widget->size() != ia_imageRect.size())
			{
				BRect realRect = ia_imageRect.align(ia_align, widget->size());
				widget->setRect(realRect);
			}
		}
		else
		{
			widget->setVisible(rect.valid());
			widget->setRect(rect);
			if (ia_align && widget->size() != rect.size())
			{
				BRect realRect = rect.align(ia_align, widget->size());
				widget->setRect(realRect);
			}
		}

	}
}
